﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;
using System.Collections;


namespace NLLSite.Administrator.UserControl
{
    public partial class AddNewCheckIn : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) { 
                for(int month=1; month<=13; month++){
                    ddlMonth.Items.Add(new ListItem(month.ToString(), month.ToString()));
                }
                for (int year = 1900; year <= 2030; year++)
                {
                    ddlYear.Items.Add(new ListItem(year.ToString(), year.ToString()));
                }
                ddlMonth.SelectedValue = DateTime.Now.Month.ToString();
                ddlYear.SelectedValue = DateTime.Now.Year.ToString();

                for (int day = 1; day <= DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedItem.Value), Convert.ToInt32(ddlMonth.SelectedItem.Value)); day++)
                {
                    ddlDay.Items.Add(new ListItem(day.ToString(), day.ToString()));
                }
            }
            
        }
        public void Reset()
        {
            txtTen.Text = string.Empty;
            txtDienthoai.Text = string.Empty;
           // dpkBirth.SelectedValue = string.Empty;
            txtEmail.Text = string.Empty;
            txtCongty.Text = string.Empty;
            txtThongtin.Text = string.Empty;
            CheckBoxList1.SelectedIndex = -1;
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            DateTime birthDay = Convert.ToDateTime((ddlYear.SelectedItem.Value + "-" + ddlMonth.SelectedItem.Value + "-" + ddlDay.SelectedItem.Value));
            HocVien h = new HocVien();
            if (CheckBoxList1.SelectedIndex == -1)
            {
                lblSuccess.Visible = true;
                lblSuccess.Text = "Bạn chưa chọn khóa học";
            }
            else
            {
                int? id = h.AddNewCheckIn(txtTen.Text, txtDienthoai.Text, birthDay, txtEmail.Text, txtCongty.Text, txtThongtin.Text);
                string khoahoc = string.Empty;
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                        lblSuccess.Text = h.AddNewReg(Convert.ToInt32(id), Convert.ToInt32(CheckBoxList1.Items[i].Value));
                        khoahoc += "<p>- " + CheckBoxList1.Items[i].Text + "</p>";
                    }
                }
                lblSuccess.Visible = true;
                string details =  "<p>Học viên: " + txtTen.Text + "</p>"
                                + "<p>Điện thoại: " + txtDienthoai.Text + "</p>"
                                + "<p>Email: " + txtEmail.Text + "</p>"
                                + "<p>Ngày sinh: " //+ dpkBirth.SelectedDate.ToString("dd - MM - yyyy") + "</p>"
                                + "<p>Khóa học đăng ký: " + khoahoc + "</p>"
                                + "<p>Nơi đang làm việc: " + txtCongty.Text + "</p>"
                                + "<p>Thông tin khác: " + txtThongtin.Text;
                h.SendEmail("Thông tin học viên đăng kí khóa học", details);
                Reset();
                Panel1.Visible = false;
            }
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int day = 1; day <= DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedItem.Value), Convert.ToInt32(ddlMonth.SelectedItem.Value)); day++)
            {
                ddlDay.Items.Add(new ListItem(day.ToString(), day.ToString()));
            }
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int day = 1; day <= DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedItem.Value), Convert.ToInt32(ddlMonth.SelectedItem.Value)); day++)
            {
                ddlDay.Items.Add(new ListItem(day.ToString(), day.ToString()));
            }
        }
    }
}